﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using SCRUM.Core.BusinessObjects;
using SCRUM.Core.DAO;
using System.Collections.Specialized;   // string dictionary

/// <summary>
/// This widget allows user to manipulate with any backlog item.
/// User can: filter backlog items, create/edit/delete backlog item.
/// </summary>
public partial class BliWidget : DataOverviewWidgetWithDetails<BLI,UserStory>
{

    #region [ BASE ]

    /// <summary>
    /// Gets the name of the widget. It must be exactly the same as the folder that contains the widget.
    /// </summary>
    /// <value>BliWidget</value>
    public override string Name
    {
        get { return "BliWidget"; }
    }

    /// <summary>
    /// Gets the title. Title is shown in the header of the widget container.
    /// </summary>
    /// <value>The title.</value>
    public override string Title
    {
        get { return (string)GetLocalResourceObject("Title"); }
    }

    #endregion

    #region [ OVERRIDEN ]

    public void LoadWidget2(object sender, EventArgs e)
    {
        LoadWidget();
    }

    /// <summary>
    /// This method works as a substitute for Page_Load. You should use this method for
    /// data binding etc. instead of Page_Load.
    /// </summary>
    public override void LoadWidget()
    {
        LoadAvailableColumns();
        ApplySettings();
        if (!IsPostBack)
        {
            RefreshWidget();
        }
    }

    /// <summary>
    /// List of items after filterring
    /// </summary>
    protected override IList<BLI> itemsAcordingToFilter
    {
        get
        {
            int bliID = (int)this.FilterID.Value == 0 ? -1 : (int)this.FilterID.Value;
            int userStoryID = this.UserStories.SelectedUserStoryID;
            int sprintID = this.UserStories.SelectedSprintID;
            int releaseID = this.UserStories.SelectedReleaseID;
            int projectID = this.UserStories.SelectedProjectID;
            string bliName = String.IsNullOrEmpty(this.FilterName.Text) ? null : this.FilterName.Text;
            int stateID = this.FilterState.SelectedStateID;
            return DAOBli.GetBlis(projectID, releaseID, sprintID, userStoryID, bliID, -1, stateID, bliName);
            
        }
    }
    #endregion

    #region [ SETTINGS ]

    private const int bliColumnOffset = 2;

    /// <summary>
    /// Gets a value indicating whether this instance is editable.
    /// </summary>
    /// <value>
    /// 	<c>true</c> if this instance is editable; otherwise, <c>false</c>.
    /// </value>
    public override bool IsEditable
    {
        get { return true; }
    }

    /// <summary>
    /// Shows the edit.
    /// </summary>
    public override void ShowEdit()
    {
        base.ShowEdit();
        this.EditBliWidget.Visible = true;
    }

    /// <summary>
    /// Hides the edit.
    /// </summary>
    public override void HideEdit()
    {
        base.HideEdit();
        this.EditBliWidget.Visible = false;
    }

    /// <summary>
    /// Occurs when user clicks on Save Settings of PrototypeWidget
    /// </summary>
    /// <param name="sender">The source of the event.</param>
    /// <param name="e">The <see cref="System.Web.UI.WebControls.GridViewPageEventArgs"/> instance containing the event data.</param>
    protected void SaveSettingsClick(object sender, EventArgs e)
    {
        SaveSettings();
        this.HideEdit();
        Response.Redirect(Request.Url.ToString());
        //LoadWidget();
    }
    #endregion

    #region [ ITEM MANIPULATION ]
    protected override void CreateItem()
    {
        BliDetail.CreateItem();
        ShowOperationResult(MessageType.Info, "BliCreated");
    }

    protected override void SaveItem()
    {
        BliDetail.EditItem(selectedItem.ID);
        ShowOperationResult(MessageType.Info, "BliUpdated");
    }

    protected override void DeleteItem(int itemID)
    {
        DAOBli.DeleteBli(itemID);
        ShowOperationResult(MessageType.Info, "BliDeleted");
    }
    #endregion

    #region [inherited controls mapping]
    // DataOverviewWidget
    protected override TextBox textBoxPageSize { get { return this.TextBoxPageSize; } }
    protected override Panel availableColumnsPanel { get { return this.AvailableColumns; } }
    protected override GridView data { get { return this.GridViewData; } }
    protected override int alwaysVisibleColumn { get { return bliColumnOffset; } }
    // DataOverviewWidgetWithDetails
    protected override Button buttonCreate { get { return this.ButtonCreate; } }
    protected override Button buttonCreateItem { get { return this.ButtonCreateItem; } }
    protected override Button buttonSaveItem { get { return this.ButtonSaveItem; } }
    protected override Button buttonCancelItem { get { return this.ButtonCancelItem; } }
    protected override Button buttonDeleteItem { get { return this.ButtonDeleteItem; } }
    protected override HierarchicalObjectDetails<BLI, UserStory> itemDetails { get { return this.BliDetail; } }
    protected override int filtersSuperiorItemID { get { return this.UserStories.SelectedUserStoryID; } }
    protected override int filtersProjectID { get { return this.UserStories.SelectedProjectID; } }
    #endregion
}
